<?php

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Models\Trans\Refunds\RefundsNotifyMessage;
use JLPay\Utils\Logger;

/**
 * 交易 - 退款通知处理功能使用示例
 */

// =================== 工具函数 =====================
/**
 * 处理退款通知
 *
 * @param array $notifyData 通知数据数组
 * @return array 处理结果数组
 */
function processRefundsNotify(array $notifyData): array
{
    $logger = Logger::getInstance();
    $logger->info("开始处理退款通知");
    
    try {
        // 创建退款通知消息对象
        $notifyMessage = new RefundsNotifyMessage($notifyData);
        
        $logger->info("收到退款通知");
        $logger->info("通知报文：" . json_encode($notifyMessage->toArray(), JSON_UNESCAPED_UNICODE));
        
        // 根据状态处理不同的业务逻辑
        switch ($notifyMessage->getStatus()) {
            case "1":
                $logger->info("退款状态：待确认");
                // 处理待确认状态
                break;
            case "2":
                $logger->info("退款状态：成功");
                // 处理成功状态
                // 可以更新本地订单状态、发送成功通知等
                break;
            case "3":
                $logger->info("退款状态：失败");
                // 处理失败状态
                // 可以记录失败原因、重试等
                break;
            default:
                $logger->warning("未知状态：" . $notifyMessage->getStatus());
        }
        
        // 返回成功响应
        $result = ["code" => "00", "message" => "处理成功"];
        $logger->info("处理结果：" . json_encode($result, JSON_UNESCAPED_UNICODE));
        return $result;
        
    } catch (Exception $e) {
        $logger->error("处理退款通知失败：" . $e->getMessage());
        // 返回失败响应
        $result = ["code" => "99", "message" => "处理失败"];
        $logger->error("处理结果：" . json_encode($result, JSON_UNESCAPED_UNICODE));
        return $result;
    }
}

/**
 * 构建模拟的退款通知数据
 *
 * @return array 模拟的通知数据数组
 */
function buildMockNotifyData(): array
{
    return [
        "status" => "2",  // 成功
        "summary" => "退款成功",
        "mch_id" => "849584358120018",
        "transaction_id" => "451147688260420350771289",
        "out_trade_no" => "REFUNDS_TEST_001",
        "chn_transaction_id" => "CHN123456789",
        "ori_out_trade_no" => "TEST_ORDER_001",
        "ori_transaction_id" => "80011557201909171853011568",
        "total_fee" => "100",
        "trans_time" => "20241230143000",
        "success_time" => "20241230143100"
    ];
}

// =================== 示例函数 =====================
/**
 * 退款通知处理示例
 */
function refundsNotifyDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行退款通知处理示例");
    
    // 构建模拟的通知数据
    $mockNotifyData = buildMockNotifyData();
    
    // 处理退款通知
    $result = processRefundsNotify($mockNotifyData);
    
    $logger->info("最终处理结果：" . json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    $logger->info("退款通知处理示例执行完毕");
}

// =================== 主程序 =====================
$logger = Logger::getInstance();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 退款通知处理示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

refundsNotifyDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 退款通知处理示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 